package edu.sctu.liangyuxiang.day20150619;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by user20 on 2015/6/19.
 */
public class UrlConnection {

    public void urlConnectionGet(String URL) throws Exception{
        URL url = new URL(URL);
        URLConnection urlConnection = url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        byte[] buffer = new byte[1024];
        int count;

        while ((count = inputStream.read(buffer))!=-1){
            System.out.println(new String(buffer,0,count));
        }
    }

    public void urlConnectionPost(String URL, String param )throws Exception{
        PrintWriter out = null;
        BufferedReader in = null;

        URL url = new URL(URL);
        URLConnection urlConnection = url.openConnection();
//      urlConnection.setRequestProperty(key,value);
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);

        out = new PrintWriter(urlConnection.getOutputStream());
        out.print(param);
        out.flush();
        in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        String line;

        while ((line = in.readLine())!=null){
            System.out.println(line);
        }

        out.close();
        in.close();

    }
}
